package com.it.tydic.estate.module.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.common.cache.DictionaryCache;
import com.it.tydic.estate.common.util.CommonUtils;
import com.it.tydic.estate.common.util.CurrentUtil;
import com.it.tydic.estate.common.util.StringUtils;
import com.it.tydic.estate.module.mapper.inner.*;
import com.it.tydic.estate.module.model.Vo.CustomerProfileVo;
import com.it.tydic.estate.module.model.inner.*;
import com.it.tydic.estate.module.service.CustomerProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @description: 客户档案
 * @author: liuhui
 * @createDate: 2020/3/2
 * @version: 1.0
 */
@Service
@Transactional
public class CustomerProfileServiceImpl implements CustomerProfileService {
    @Autowired
    private TbCustomerProfileMapper tbCustomerProfileMapper;    /*档案*/
    @Autowired
    private TbMoveInMapper tbMoveInMapper;  /*迁入*/
    @Autowired
    private TbInformationMapper tbInformationMapper;    /*中间*/
    @Autowired
    private TbMoveOutMapper tbMoveOutMapper;    /*迁出*/
    @Autowired
    private FangjianguanliMapper fangjianguanliMapper;  /*房间*/

    /**
     * 客户档案列表查询
     * @return
     */
    @Override
    public Result selectAllList(Map<String,Object> customerProfileMap) {
        /*分页*/
//        if(customerProfileMap.containsKey("pageNum") && customerProfileMap.containsKey("pageSize")){//判断是否有分页条件有进行分页
//            PageHelper.startPage((Integer) customerProfileMap.get("pageNum"), (Integer) customerProfileMap.get("pageSize"));
//        }
        CommonUtils.pageProcess(customerProfileMap);//验证处理分页
        Page<TbCustomerProfile> tbCustomerProfiles = tbCustomerProfileMapper.selectLink(customerProfileMap);
        List<TbCustomerProfile> result = tbCustomerProfiles.getResult();
        /*返回集合*/
        Map<String,Object> map = new HashMap<>();
        if(result.size()>0 && result != null){
            /*码表赋值*/
            for (TbCustomerProfile tbCustomerProfile : result) {
                tbCustomerProfile.setAttributes(DictionaryCache.getDicVal( tbCustomerProfile.getAttributes()));
                tbCustomerProfile.setmType(DictionaryCache.getDicVal(tbCustomerProfile.getmType()));
            }
        }
        map.put("result",result);
        map.put("totle",tbCustomerProfiles.getTotal());
        return Result.success(map);
    }

    /**
     * 获取单体信息
     * @param uid
     * @return
     */
    @Override
    public Result getById(String uid) {
        /*参数验证*/
        if(StringUtils.isNotEmpty(uid)){
            Map<String, Object> map = new HashMap<>();
            map.put("uid",uid);
            List<TbCustomerProfile> result = tbCustomerProfileMapper.selectLink(map).getResult();
            return Result.success(result);
        }
        return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
    }

    /**
     * 客户档案修改
     * @return
     */
    @Override
    public Result updataTbCustomerProfile(TbCustomerProfile tbCustomerProfile) {
        /*创建修改人*/
        tbCustomerProfile.setUuserId(CurrentUtil.getCurrentUserId());
        /*修改时间*/
        tbCustomerProfile.setXgsj(new Date());
        return  tbCustomerProfileMapper.updateBatch(tbCustomerProfile) >0 ? Result.success():Result.error(CodeMsg.UPDATE_ERROR);
    }

    /**
     *  获取房间树
     * @param lyId
     * @return
     */
    @Override
    public Result getRoomTree(String lyId) {
        /*参数集合*/
        Map<String,Object> map = new HashMap<>();
        /*返回集合*/
        Map<String,Object> resultMap = new HashMap<>();
        /*数据存放集合*/
        List<Object>result = new ArrayList<>();
        map.put("lyid",lyId);
        Page<Fangjianguanli> fangjianguanlis = fangjianguanliMapper.selectList(map);
        List<Fangjianguanli> fjResult = fangjianguanlis.getResult();
        for (Fangjianguanli fangjianguanli : fjResult) {  /*获取房间id*/
            map.put("roomId",fangjianguanli.getId());
            /*获取用户信息*/
            Page<TbCustomerProfile> tbCustomerProfiles = tbCustomerProfileMapper.selectLink(map);
            List<TbCustomerProfile> roomProfiles = tbCustomerProfiles.getResult();
            StringBuffer name = new StringBuffer();
            name.append(fangjianguanli.getFjbh());
            for (TbCustomerProfile roomProfile : roomProfiles) {
                if(roomProfile.getUserName()!= null){
                    name.append("|"+roomProfile.getUserName());
                }
            }
            CustomerProfileVo customerProfileVo = new CustomerProfileVo();
            customerProfileVo.setTbCustomerProfile(roomProfiles);
            customerProfileVo.setName(name.toString());
            customerProfileVo.setId(fangjianguanli.getId());
            customerProfileVo.setLyId(lyId);
            result.add(customerProfileVo);
        }
        resultMap.put("result",result);
        resultMap.put("total",fangjianguanlis.getTotal());
        return Result.success(resultMap);
    }

    /**
     * 获取管理处
     * @param userCode
     * @return
     */
    @Override
    public Result getGLC(String userCode) {
        /*参数验证*/
        if(StringUtils.isNotEmpty(userCode))
            return Result.success(tbCustomerProfileMapper.getGLC(userCode));
        return Result.error(CodeMsg.EMPTY_PARAM_ERROR);
    }
}
